package dk.simonvogensen.itodo.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import dk.simonvogensen.itodo.client.controller.Controller;
import dk.simonvogensen.itodo.shared.model.TagItem;
import dk.simonvogensen.uirecorder.client.RecordableButton;
import dk.simonvogensen.uirecorder.client.RecordableFlexTable;
import dk.simonvogensen.uirecorder.client.RecordableLabel;
import dk.simonvogensen.uirecorder.client.RecordableVerticalPanel;

import java.util.Iterator;
import java.util.List;

/**
 * @author $LastChangedBy:$ $LastChangedDate:$
 * @version $Revision:$
 */
public class TagsDialogBox extends DialogBox {
    public TagsDialogBox(final Controller controller, final View view, String tag, List tagItemList) {
        super(false, false);
        setText("Tag: " + tag);
        VerticalPanel panel = new RecordableVerticalPanel();

        FlexTable table = new RecordableFlexTable();
        int row = 0;
        String lastHeaderText = "";
        for (Iterator it = tagItemList.iterator(); it.hasNext();) {
            final TagItem item = (TagItem) it.next();
            if (!lastHeaderText.equals(item.getTodoItem().getHeaderText())) {
                Label label = new RecordableLabel(item.getTodoItem().getShortHeaderText());
                label.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        controller.selectTodo(item.getTodoItem());
                    }
                });
//                    DOM.setStyleAttribute(label.getElement(), "color", "DDDDFF");
                table.setWidget(row, 0, label);
                lastHeaderText = item.getTodoItem().getHeaderText();
            }
//            table.setWidget(row++, 1, new RecordableLabel(item.getText()));
        }
        panel.add(table);

        Button ok = new RecordableButton("Close", new ClickHandler() {
            public void onClick(ClickEvent event) {
                TagsDialogBox.this.hide();
//                view.tagsListBox.setSelectedIndex(0);
            }
        });
        panel.add(ok);
        panel.setCellHorizontalAlignment(ok, HorizontalPanel.ALIGN_CENTER);
        setWidget(panel);
    }
}

